// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.action;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.ExceptionOperation;
import org.dengues.core.actions.AbstractAction;
import org.dengues.core.warehouse.ENavNodeType;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.warehouse.ProcessType;
import org.dengues.model.warehouse.ScriptsStorage;
import org.dengues.warehouse.i18n.Messages;
import org.dengues.warehouse.models.FolderWarehouseNode;
import org.dengues.warehouse.viewers.csv.NewCSVFileWizard;
import org.dengues.warehouse.viewers.database.NewDatabaseWizard;
import org.dengues.warehouse.viewers.excel.NewExcelFileWizard;
import org.dengues.warehouse.viewers.folder.NewFolderWizard;
import org.dengues.warehouse.viewers.process.NewProcessWizard;
import org.dengues.warehouse.viewers.script.NewJavaScriptsWizard;
import org.dengues.warehouse.viewers.wizards.WizardDialogI18N;
import org.dengues.warehouse.viewers.xml.NewXmlFileWizard;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-28 qiang.zhang $
 * 
 */
public class EditPropertiesAction extends AbstractAction {

    /**
     * Qiang.Zhang.Adolf@gmail.com EditPropertiesAction constructor comment.
     */
    public EditPropertiesAction() {
        super(Messages.getString("EditPropertiesAction.editproperties")); //$NON-NLS-1$
        setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.SQLEDITOR_EDIT));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        try {
            IWarehouseNode warehouseNode = pnodes.get(0);
            boolean isActived = ENodeStatus.ACTIVED.equals(warehouseNode.getNodeStatus());
            IPath path = getPath();
            if (warehouseNode instanceof FolderWarehouseNode) {
                NewFolderWizard wizard = new NewFolderWizard(warehouseNode.getCategoryName(), path, warehouseNode.getLabel());
                WizardDialog dialog = new WizardDialogI18N(Display.getCurrent().getActiveShell(), wizard);
                if (dialog.open() == WizardDialog.OK) {
                    refresh(pnodes.get(0));
                }
            } else {
                boolean isQuery = warehouseNode.getCategoryName().compareTo(ENodeCategoryName.QUERY) == 0;
                Wizard wizard = null;
                if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.DATABASE) == 0 || isQuery) {
                    DatabaseDiagram data = (DatabaseDiagram) warehouseNode.getObject().getData();
                    wizard = new NewDatabaseWizard(data, path, isActived, isQuery);
                } else if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.PROCESS) == 0
                        || warehouseNode.getCategoryName().compareTo(ENodeCategoryName.BLOCKS) == 0) {
                    ProcessType data = (ProcessType) warehouseNode.getObject().getData();
                    wizard = new NewProcessWizard(data, path, isActived);
                } else if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.SCRIPTS) == 0) {
                    ScriptsStorage data = (ScriptsStorage) warehouseNode.getObject().getData();
                    wizard = new NewJavaScriptsWizard(data, path, isActived);
                } else if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.XMLFILE) == 0) {
                    DatabaseDiagram data = (DatabaseDiagram) warehouseNode.getObject().getData();
                    wizard = new NewXmlFileWizard(data, path, isActived);
                } else if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.CSVFILE) == 0) {
                    DatabaseDiagram data = (DatabaseDiagram) warehouseNode.getObject().getData();
                    wizard = new NewCSVFileWizard(data, path, isActived);
                } else if (warehouseNode.getCategoryName().compareTo(ENodeCategoryName.EXCELFILE) == 0) {
                    DatabaseDiagram data = (DatabaseDiagram) warehouseNode.getObject().getData();
                    wizard = new NewExcelFileWizard(data, path, isActived);
                }
                if (wizard != null) {
                    WizardDialog dialog = new WizardDialogI18N(Display.getCurrent().getActiveShell(), wizard);
                    if (dialog.open() == WizardDialog.OK) {
                        refresh(pnodes.get(0));
                    }
                }
            }
        } catch (Exception e) {
            ExceptionOperation.operate(e);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.actions.AbstractAction#getLevel()
     */
    @Override
    public int getLevel() {
        return 200;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.actions.AbstractAction#computeEnable()
     */
    @Override
    protected boolean computeEnable() {
        boolean isEnable = false;
        if (pnodes != null && pnodes.size() == 1) {
            isEnable = (pnodes.get(0).getCategoryName() != ENodeCategoryName.TABLE && pnodes.get(0).getCategoryName() != ENodeCategoryName.COLUMN)
                    && (pnodes.get(0).getNodeType() == ENavNodeType.SIMPLE_FOLDER || pnodes.get(0).getNodeType() == ENavNodeType.ELEMENT)
                    && !pnodes.get(0).isBin();
        }
        return isEnable;
    }

}
